/*
xp_uart.c
offer some userfull function for uart driver
designed by lunanting

-------------------------
2020-06-13 16:49:01
create file
-------------------------
*/
#include "xp_uart.h"


/*
package alios uart recived data function
recive data wait forever,time out when recived data
param:
    uart:uart object
    rec_buf:recive buffer pointer
    max_rec_len:recived buffer max length
    rec_len:recived length
    timeout: timeout time :ms
return:
    0=success
*/
void xp_uart_recive(uart_dev_t *uart,u8 *buf,int max_rec_len,int *rec_len,int timeout){
    int wait=HAL_WAIT_FOREVER;
    int offset=0;
    u32 len;
    *rec_len=0;
    while(1){
        if(hal_uart_recv_II(uart,buf+offset,1,&len,wait)==0){
            wait=timeout;
            if(offset==max_rec_len-1)break;
        }
        else{
            break;
        }
        offset++;
    }
    *rec_len=offset;
}



/*
clear uart recive buffer
param:
    uart:uart object
    timeout:read time
*/
void xp_uart_rec_clear(uart_dev_t *uart){
    u8 buf[100];
    u32 rec_len;
    while(1){
        if(hal_uart_recv_II(uart,&buf,100,&rec_len,20)==0){
            continue;
        }
        else{
            break;
        }
    }
}


//end of the file
